package socket;
/**
 * 聊天室客户端
 */

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Client {
    private Socket socket;
    public Client(){
        try {
            System.out.println("正在连接服务端");
            socket = new Socket("localhost",8088);  //3次握手
            System.out.println("服务端连接成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {
            OutputStream out  = socket.getOutputStream();  //获取网络输出流
            byte[] data = "hello,server!".getBytes(StandardCharsets.UTF_8);  //获取字节数组
            out.write(data);  //将字节数组写出去
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();  //进行四次挥手
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
